<?php
	include_once("conn.php");

	if(isset($_GET["tid"]))
	{
		$delete_tid = $_GET["tid"];		//欲删除的主贴的ID
		$back_board = $_GET["back_board"];
		//删主贴之前，肯定要先删主帖下的所有回帖

		$sql = "delete from bbs_reply where r_tid = '".$delete_tid."'";
		$result = mysql_query($sql);
		if(!$result)
		{
			echo "<script>alert('此主题的回帖删除失败!');window.location = 'browse.php?board=".$back_board."'</script>";
		}

		$sql = "delete from bbs_topic where t_id = '".$delete_tid."'";
		$result = mysql_query($sql);
		if($result)
		{
			echo "<script>window.location = 'page.php?board=".$back_board."'</script>";
		}
		else {
			echo "<script>alert('删除主帖失败!');window.location = 'browse.php?board=".$back_board."&tid=".$delete_tid."'</script>";
		}
	}
	//下面是删除回帖的
	if(isset($_GET["rid"]))
	{
		$back_board = $_GET["back_board"];	//要利用这地址跳回browse.php
		$delete_rid = $_GET["rid"];		//欲删除的回帖ID



		$sql3 = "select r_tid,r_time from bbs_reply where r_id = '".$delete_rid."'";
		$result3 = mysql_query($sql3);
		$q_result3 = mysql_fetch_array($result3);
		$r_tid = $q_result3["r_tid"];		//找出此条回帖的主帖ID
		$r_time = $q_result3["r_time"];		//本条欲删回贴的发表时间，需要的都取完了，下面就将这条删掉。


		$sql2 = "delete from bbs_reply where r_id = '".$delete_rid."'";
		$result2 = mysql_query($sql2);


		$sql = "select t_last_reply_time from bbs_topic where t_id = '".$r_tid."'";
		$result = mysql_query($sql);
		$q_result = mysql_fetch_array($result);
		$t_last_reply_time = $q_result["t_last_reply_time"];
		if($r_time == $t_last_reply_time)		//为1 则证明 此条删掉的回贴为尾楼，即最新回复;若不是最后一层楼则不必更改最新回复时间/人
		{
			//这时候，最新回复和最新时间都要换；
			$sql4 = "select r_time,r_u_name from bbs_reply where r_tid = '".$r_tid."' order by r_time desc limit 1";
			$result4 = mysql_query($sql4);
			$q_result4 = mysql_fetch_array($result4);
			$t_last_reply_time = $q_result4["r_time"];
			$t_last_reply_name = $q_result4["r_u_name"];

			$sql = "update bbs_topic set t_last_reply_time = '".$t_last_reply_time."',t_last_reply_name = '".$t_last_reply_name."' where t_id = '".$r_tid."'";
			$result = mysql_query($sql);
			if(!$result) {
				echo "<script>alert('更新最新回复人/时间失败');</script>";
			}

		}


		//通过主帖ID 找出主贴的最后回复时间、最后回复人
		

		if($result2)
		{
			echo "<script>window.location = 'browse.php?board=".$back_board."&tid=".$r_tid."';</script>";
		}
		else {
			echo "<script>alert('删除回帖失败');window.location = 'browse.php?board=".$back_board."&tid=".$r_tid."';</script>";
		}
	}


?>